/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client.dialog;

import org.ajiaojr.enigmagm.client.UserSessionManager;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author ajiao
 */
public class LoginDialog extends DialogBox {
  private static final LoginDialogMessages MESSAGES =
      (LoginDialogMessages) GWT.create(LoginDialogMessages.class);

  private PasswordTextBox passwordTextBox;

  private CheckBox rememberMeCheckBox;

  private TextBox usernameTextBox;

  public LoginDialog() {
    super(false);
    setHTML(MESSAGES.title());

    final VerticalPanel verticalPanel = new VerticalPanel();
    setWidget(verticalPanel);
    verticalPanel.setWidth("260px");

    final HorizontalPanel horizontalPanel = new HorizontalPanel();
    verticalPanel.add(horizontalPanel);
    horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    horizontalPanel.setSpacing(5);
    horizontalPanel.setWidth("100%");

    final Label label = new Label(MESSAGES.username());
    horizontalPanel.add(label);
    horizontalPanel.setCellHorizontalAlignment(label,
        HasHorizontalAlignment.ALIGN_RIGHT);
    horizontalPanel.setCellWidth(label, "80px");

    usernameTextBox = new TextBox();
    horizontalPanel.add(usernameTextBox);

    String username = Cookies.getCookie("USERNAME");
    if (username != null) {
      usernameTextBox.setText(username);
    }

    final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
    verticalPanel.add(horizontalPanel_1);
    horizontalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    horizontalPanel_1.setSpacing(5);
    horizontalPanel_1.setWidth("100%");

    final Label label_1 = new Label(MESSAGES.password());
    horizontalPanel_1.add(label_1);
    horizontalPanel_1.setCellHorizontalAlignment(label_1,
        HasHorizontalAlignment.ALIGN_RIGHT);
    horizontalPanel_1.setCellWidth(label_1, "80px");

    passwordTextBox = new PasswordTextBox();
    horizontalPanel_1.add(passwordTextBox);

    final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
    verticalPanel.add(horizontalPanel_3);
    verticalPanel.setCellVerticalAlignment(horizontalPanel_3,
        HasVerticalAlignment.ALIGN_TOP);
    horizontalPanel_3.setWidth("100%");

    rememberMeCheckBox = new CheckBox();
    horizontalPanel_3.add(rememberMeCheckBox);
    horizontalPanel_3.setCellVerticalAlignment(rememberMeCheckBox,
        HasVerticalAlignment.ALIGN_MIDDLE);
    horizontalPanel_3.setCellHorizontalAlignment(rememberMeCheckBox,
        HasHorizontalAlignment.ALIGN_CENTER);
    rememberMeCheckBox.setText(MESSAGES.rememberMe());

    final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
    verticalPanel.add(horizontalPanel_2);
    horizontalPanel_2.setSpacing(5);
    verticalPanel.setCellHorizontalAlignment(horizontalPanel_2,
        HasHorizontalAlignment.ALIGN_CENTER);

    final Button button = new Button();
    horizontalPanel_2.add(button);
    button.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        UserSessionManager.getInstance().login(usernameTextBox.getText(),
            passwordTextBox.getText(), rememberMeCheckBox.isChecked());
        LoginDialog.this.hide();
      }
    });
    button.setText(MESSAGES.login());

    final Button button_1 = new Button();
    horizontalPanel_2.add(button_1);
    button_1.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        LoginDialog.this.hide();
      }
    });
    button_1.setText(MESSAGES.cancel());

    setPopupPosition((Window.getClientWidth() - 260) / 2, (Window
        .getClientHeight() - 300) / 2);
  }
}
